<?php

class MResource extends CI_Model{

	public function __construct()
	    {
	        parent::__construct();
	    }
	
   function getFileType(){
		$data = array();
		$this->db->select('file_type_id,type');
	    $Q = $this->db->get('file_type');
	    if ($Q->num_rows() > 0){
	      foreach ($Q->result_array() as $row){
	         $data[$row['file_type_id']] = $row['type'];
	       }
	    }
	    $Q->free_result();  
	    return $data;   
	 }
	 
	function getFileTypeById($id){
		$data = array();
		$this->db->select('type');
		$this->db->where('file_type_id',id_clean($id));
	    $this->db->limit(1);
	    $Q = $this->db->get('file_type');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	    $Q->free_result();  
	    return $data;   
	 }
	    
	 function getAllResources(){
	     $data = array();
	     $this->db->select('file.*,file_type.type as file_type, shift.title as shift, course.title as course');
	     $this->db->from('file');
	      $this->db->join('file_type','file.file_type_id = file_type.file_type_id');
	     $this->db->join('shift', 'file.shift_id = shift.shift_id');
	     $this->db->join('course','file.course_id = course.course_id');
	     $this->db->order_by("file.date_created", "desc");
	     $Q = $this->db->get();
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data; 
	 }
	
	 function getResource($id){
	 	$data = array();
	    $this->db->where('file_id',id_clean($id));
	    $this->db->where('uid',id_clean($_SESSION['uid']));
	    $this->db->limit(1);
	    $Q = $this->db->get('file');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;    
	 }
		
	 function addResource(){
		$data = array( 
			'file_type_id' => db_clean($_POST['file_type_id']),
			'semester' => db_clean($_POST['semester']),
			'shift_id' => db_clean($_POST['shift_id']),
			'group' => $_POST['group'],
			'batch' => db_clean($_POST['batch']),
			'year' => db_clean($_POST['year']),
			'uid' => db_clean($_SESSION['uid']),
			'course_id' => db_clean($_POST['course_id']),
			'activate' => db_clean($_POST['activate'])
		);
		if ($_FILES){
		 	$path = 'images/'.$_SESSION['username'];
		 	if(!is_dir($path))
		    {
		      mkdir($path,0755,TRUE);
		    } 
		    
			$config['upload_path'] = './'.$path;
			$config['allowed_types'] = '*';// 'gif|jpg|png|flv|mp4|pdf|ppt';
			$config['max_size'] = '524288';//512 mb
			$config['remove_spaces'] = true;
			$config['overwrite'] = false;
			$config['max_width']  = '0';
			$config['max_height']  = '0';
			$this->load->library('upload', $config);	
		
			if (strlen($_FILES['file_src']['name'])){
				if(!$this->upload->do_upload('file_src')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
			
				if ($image['file_name']){
					$data['file_src'] = $image['file_name'];
				}
			}
		}
		
		$this->db->set('date_created', 'NOW()', FALSE);
		$this->db->insert('file', $data);	 
	 }
	 
	 function updateResource(){
		$data = array( 
			'file_type_id' => db_clean($_POST['file_type_id']),
			'semester' => db_clean($_POST['semester']),
			'shift_id' => db_clean($_POST['shift_id']),
			'group' => $_POST['group'],
			'batch' => db_clean($_POST['batch']),
			'year' => db_clean($_POST['year']),
			'uid' => db_clean($_SESSION['uid']),
			'course_id' => db_clean($_POST['course_id']),
			'activate' => db_clean($_POST['activate'])
		);
		 if ($_FILES['file_src']['name'] != null || $_FILES['file_src']['name'] !=""){
		 	$source = $this->db->where('file_id', id_clean($_POST['file_id']))->get('file')->row();
		 	$path = 'images/'.$_SESSION['username'];
    		if (unlink($path.'/'.$source->file_src)) {
    			
    		}else {
    			$this->session->set_flashdata('error', 'can not delete image from directory.');
    		}
		 	$config['upload_path'] = './'.$path;
			$config['allowed_types'] = '*';// 'gif|jpg|png|flv|mp4|pdf|ppt';
			$config['max_size'] = '524288';//512 mb
			$config['remove_spaces'] = true;
			$config['overwrite'] = false;
			$config['max_width']  = '0';
			$config['max_height']  = '0';
			$this->load->library('upload', $config);	
		
			if (strlen($_FILES['file_src']['name'])){
				if(!$this->upload->do_upload('file_src')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
			
				if ($image['file_name']){
					$data['file_src'] = $image['file_name'];
				}
			}
    		
		 }
	 	$this->db->where('file_id', id_clean($_POST['file_id']));
	 	$this->db->where('uid', id_clean($_SESSION['uid']));
	 	$this->db->set('date_modified', 'NOW()', FALSE);
		$this->db->update('file', $data);	
	 
	 }
	 
	 
	 function inactiveResource($id){
	 	$data = array('activate' => 0);
	 	$this->db->where('file_id', id_clean($id));
		$this->db->update('file', $data);	
	 }
	 
	 function deleteResource($id){
	 	$source = $this->db->where('file_id', id_clean($id))->get('file')->row();
	 	$path = 'images/'.$_SESSION['username'];
	 	if($source->file_src != null){
		 	if (file_exists(FCPATH.$path.'/'.$source->file_src)) {
		 		unlink($path.'/'.$source->file_src);
	    	}else {
	    		$this->session->set_flashdata('error', 'can not delete image from directory.');
	    	}
	 	}	 	
	 	$this->db->where('file_id', id_clean($id));
	 	$this->db->where('uid', id_clean($_SESSION['uid']));
		$this->db->delete('file');	
	 }
	 
	
}

?>